import React, { Component } from 'react'

export default class item extends Component {
    state = {mouse:false}
    handleMouse = (flag) => {
        return () => {
            this.setState({mouse:flag})
        }
    }
    handleChange = (id) =>{
        return (event) => {
            this.props.UpdateTodo(id,event.target.checked)
        }
    }
    handleDelete = (id) =>{
       if(window.confirm('确定删除吗？')){
        this.props.DeleteTodo(id)
       }
       
    }
    render() {
        const {todo} = this.props
        const {mouse} =this.state
        return (
            <li style={{backgroundColor:mouse?'#ddd':'white'}}  onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={todo.done} onChange={this.handleChange(todo.id)}/>
                    <span>{todo.name}</span>
                </label>
                <button className="btn btn-danger" style={{display:mouse?'block':'none'}} onClick={()=>this.handleDelete(todo.id)}>删除</button>
            </li>
        )
    }
}
